using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using OpenComposite.Repository.Properties;
using OpenComposite.EII.Forms;
using System.IO;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class UCDomain : RepositoryItemUI
	{
		#region Initialization

		public UCDomain(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
			InitializeComponent();

			init();
		}
		private void init()
		{
			_domain = null;
		}

		#endregion

		#region Public Members

		#region Properties
		public override IRepositoryItem RepositoryItem
		{
			get
			{
				save();
				return _domain;
			}
			set
			{
				base.RepositoryItem = value;
				if ( value is Domain ) {
					_domain = (Domain)value;
					load();
				}
			}
		}

		public override string TypeText
		{
			get { return Resources.sDomain; }
		}

		public override string TypeDescription
		{
			get { return ""; }
		}
		#endregion

		#region Events
		#endregion

		#region Methods
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private Domain _domain;
		#endregion

		#region Methods

		private void load()
		{
			if ( _domain == null )
				throw new NullReferenceException("No domain defined.");

			txtDomainNm.Text = _domain.Name;
			txtDomainDesc.Text = _domain.Description;

			getDomainObjects();
		}

		private void getDomainObjects()
		{
			tabCtrlDomObjects.TabPages.Clear();
			DataTable dtFolders = Global.Data.GetRepositoryHierarchyChildren(_domain);
			if ( dtFolders == null || dtFolders.Rows.Count == 0 ) {
				groupDomObjects.Visible = false;
				return;
			}
			foreach ( DataRow row in dtFolders.Rows ) {
				if ( (int)row["child_count"] > 0 && (ItemType)row["child_reptype_id"] == ItemType.GenericFolder ) {
					TabPage tp = new TabPage();
					tp.Text = row["child_display"].ToString();
					UCRepositoryItemList objlist = new UCRepositoryItemList(this.serviceProvider);
					objlist.Dock = DockStyle.Fill;
					objlist.FolderID = (int)row["child_id"];
					tp.Controls.Add(objlist);
					tabCtrlDomObjects.TabPages.Add(tp);
				}
			}
			if ( tabCtrlDomObjects.TabCount == 0 ) {
				groupDomObjects.Visible = false;
			} else {
				groupDomObjects.Visible = true;
			}
		}
		private void save()
		{
			if ( _domain == null )
				_domain = new Domain();

			_domain.Reload();

			_domain.Name = txtDomainNm.Text;
			_domain.Description = txtDomainDesc.Text;
		}
		#endregion

		#region Event Handlers
		#endregion

		#endregion

		protected override void NotifyPropertyChange(string property, object value)
		{
			if ( property == "Name" && value != null ) {
				txtDomainNm.Text = value.ToString();
			}
		}

	}
}
